//
//  HTTPRequest.swift
//  QualityControlSystem
//
//  Created by Daniel Hu on 12/26/16.
//  Copyright © 2016 com.mask. All rights reserved.
//

import Foundation
import Alamofire
import SVProgressHUD
import MJExtension

class HTTPRequest {
    
    class func authWith(userName: String, password: String, completion: @escaping ((name: String, token: String)) -> Void) -> Void {
        Alamofire.request(Router.login(username: userName, password: password)).responseJSON { response in
            SVProgressHUD.dismiss()
            guard response.result.isSuccess else {
                SVProgressHUD.showError(withStatus: "网络错误")
                return
            }
            guard let responseJSON = response.result.value as? [String: AnyObject] else {
                SVProgressHUD.showError(withStatus: "数据格式错误")
                return
            }
            let code = Int(responseJSON["code"] as! String)
            guard code == 1000 else {
                SVProgressHUD.showError(withStatus: "登录失败\n"+(responseJSON["msg"] as! String))
                return
            }
            let name = responseJSON["loginname"] as! String
            let token = responseJSON["token"] as! String
            completion((name, token))
        }
    }
    
    class func authQulityControlSystemWith(username: String, completion: @escaping (Dictionary<String, Any>) -> Void) {
        Alamofire.request(Router.auth(username: username)).responseJSON { response in
            guard response.result.isSuccess else {
                SVProgressHUD.showError(withStatus: "网络错误")
                return
            }
            guard let responseJSON = response.result.value as? [String: AnyObject] else {
                SVProgressHUD.showError(withStatus: "数据格式错误")
                return
            }
            completion(responseJSON["data"] as! Dictionary<String, Any>)
        }
    }
    
    class func applicationsInfo(username: String, completion: @escaping (Dictionary<String, Any>) -> Void) {
        Alamofire.request(Router.applicationsInfo(username: username)).responseJSON { (response) in
            guard response.result.isSuccess else {
                SVProgressHUD.showError(withStatus: "网络错误")
                return
            }
            guard let responseJSON = response.result.value as? [String: AnyObject] else {
                SVProgressHUD.showError(withStatus: "数据格式错误")
                return
            }
            let dataArr = responseJSON["children"]
            let modelArr = ApplicationModel.mj_objectArray(withKeyValuesArray: dataArr)
            let response = ["data": modelArr]
            completion(response)
        }
    }
    
    class func projectListWith(type: Int, page: Int, completion: @escaping (Dictionary<String, Any>?) -> Void) {
        Alamofire.request(Router.projectList(type: type, page: page)).responseJSON { response in
            guard response.result.isSuccess else {
                SVProgressHUD.showError(withStatus: "网络错误")
                completion(nil)
                return
            }
            guard let responseJSON = response.result.value as? [String: AnyObject] else {
                SVProgressHUD.showError(withStatus: "数据格式错误")
                completion(nil)
                return
            }
            let state = responseJSON["status"] as! Int
            let code = responseJSON["code"] as! Int
            guard state == 200 && code == 1 else {
                SVProgressHUD.showError(withStatus: responseJSON["msg"] as! String)
                completion(nil)
                return
            }
            completion(responseJSON["data"] as! Dictionary<String, Any>?)
        }
    }
    
    class func projectListWith(keyword: String, page: Int, completion: @escaping (Dictionary<String, Any>) -> Void) {
        
    }
    
    class func subProjectListWith(projectId: String, completion: @escaping ([SubProject]?) -> Void) {
        Alamofire.request(Router.subProjectList(id: projectId)).responseJSON { response in
            guard response.result.isSuccess else {
                SVProgressHUD.showError(withStatus: "网络错误")
                completion(nil)
                return
            }
            guard let responseJSON = response.result.value as? [String: AnyObject] else {
                SVProgressHUD.showError(withStatus: "数据格式错误")
                completion(nil)
                return
            }
            let state = responseJSON["status"] as! Int
            let code = responseJSON["code"] as! Int
            guard state == 200 && code == 1 else {
                SVProgressHUD.showError(withStatus: responseJSON["msg"] as! String)
                completion(nil)
                return
            }
            var subprojects = [SubProject]()
            for subproject in responseJSON["data"]?["src"] as! [AnyObject] {
                let temp = SubProject.mj_object(withKeyValues: subproject)
                temp?.status = ProcessStatus(rawValue: subproject["status"] as! Int)
                subprojects.append(temp!)
            }
            completion(subprojects)
        }
    }
    
    class func subProjectProcessWith(subId: String, completion: @escaping ([SubProjectProcess]?) -> Void) {
        Alamofire.request(Router.subProjectProcess(id: subId)).responseJSON { response in
            guard response.result.isSuccess else {
                SVProgressHUD.showError(withStatus: "网络错误")
                completion(nil)
                return
            }
            guard let responseJSON = response.result.value as? [String: AnyObject] else {
                SVProgressHUD.showError(withStatus: "数据格式错误")
                completion(nil)
                return
            }
            let state = responseJSON["status"] as! Int
            let code = responseJSON["code"] as! Int
            guard state == 200 && code == 1 else {
                SVProgressHUD.showError(withStatus: responseJSON["msg"] as! String)
                completion(nil)
                return
            }
            var processes = [SubProjectProcess]()
            for process in responseJSON["data"]?["item"] as! [AnyObject] {
                let temp = SubProjectProcess.mj_object(withKeyValues: process)
                temp?.status = ProcessStatus(rawValue: process["status"] as! Int)
                processes.append(temp!)
            }
            completion(processes)
        }
    }
    
    class func subProjectUploadAllPicture(pictures: [Data], projectId: Int, subProjectId: Int, completion: @escaping (Dictionary<String, Any>?) -> Void) {
        Alamofire.request(Router.subProjectUploadAllPicture(pictures: pictures, projectId: projectId, subProjectId: subProjectId)).responseJSON { response in
            guard response.result.isSuccess else {
                SVProgressHUD.showError(withStatus: "网络错误")
                return
            }
            guard let responseJSON = response.result.value as? [String: AnyObject] else {
                SVProgressHUD.showError(withStatus: "数据格式错误")
                return
            }
            completion(responseJSON)
        }
    }
    
    class func reformUploadPicture(processId: Int, content: String, subProjectId: Int, projectId: Int, id: Int, endTime: Int, imageId: Int, picName: String, completion: @escaping (Dictionary<String, Any>?) -> Void) {
        Alamofire.request(Router.reformUploadPicture(processId: processId, content: content, subProjectId: subProjectId, projectId: projectId, id: id, endTime: endTime, imageId: imageId, picName: picName)).responseJSON { response in
            guard response.result.isSuccess else {
                SVProgressHUD.showError(withStatus: "网络错误")
                return
            }
            guard let responseJSON = response.result.value as? [String: AnyObject] else {
                SVProgressHUD.showError(withStatus: "数据格式错误")
                return
            }
            completion(responseJSON)
        }
    }
    
    class func historyUploadListWith(processId: String, completion: @escaping (Dictionary<String, Any>?) -> Void) {
        Alamofire.request(Router.historyUploadList(processId: Int(processId)!)).responseJSON { response in
            guard response.result.isSuccess else {
                SVProgressHUD.showError(withStatus: "网络错误")
                return
            }
            guard let responseJSON = response.result.value as? [String: AnyObject] else {
                SVProgressHUD.showError(withStatus: "数据格式错误")
                return
            }
        }
    }
}
